/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : SystemConfigTask.c
  * @brief          : System Output Task
  ******************************************************************************
  * @attention
  * 本文件用于  开关量输入输出相关的处理
  * @20210327:文件模板第一次生成
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

#include "SystemConfigTask.h"

/* USER CODE END Includes */

/* Private typedef ------------------私有结构体-----------------------------------------*/
/* USER CODE BEGIN PTD */

SaveMailDef SaveMail = {0};
/* 邮 箱 控 制 块 */
struct rt_mailbox Mb_FLASH;
/* 用 于 放 邮 件 的 内 存 池 */
static char Mb_FLASH_Pool[32];

const PowDataSaveDef	PowDataInit = 
{
	0x5550,
	{
		0
	},
	0xaaa0,
};
const SetDataSaveDef	SetDataInit = 
{
	0x5551,
	{
		{
			Enable,
			Read,
			1,
			Code03,
			0,
			Typeuint16,
			AABB,
			Ratio,
			0
		},
		{
			Enable,
			Write,
			1,
			Code03,
			0,
			Typeuint16,
			AABB,
			Ratio,
			0
		},
		{
			Enable,
			Read,
			1,
			Code03,
			2,
			Typefloat,
			CCDDAABB,
			Ratio,
			0
		},
		{
			Enable,
			Write,
			1,
			Code03,
			2,
			Typefloat,
			CCDDAABB,
			Ratio,
			0
		}
	},
	0xaaa1
};

const TimDataSaveDef	TimDataInit = 
{
	0x5552,
	{
		0,
		TIME_LIST_MAX,
		{
			0
		}
	},
	0xaaa2
};

const SysDataSaveDef	SysDataInit = 
{
	0x5553,
	{
		1,
		8,
		MODE_MQCP,
    1,
    6
	},
	0xaaa3
};

const NetDataSaveDef	NetDataInit = 
{
	0x5554,
	{
		0
	},
	0xaaa4
};

const CalDataSaveDef	CalDataInit = 
{
	0x5555,
	{
		0
	},
	0xaaa5
};
/* USER CODE END PTD */

/* Private define -----------------私有宏--------------------------------*/
/* USER CODE BEGIN PD */


/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */



/* USER CODE END PM */




/* Private variables --------------私有变量-------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

void PowDataSaveInit(void)//0
{//掉电参数初始化
	PowDataSaveDef *PowDataFlash = NULL;
	PowDataFlash = (PowDataSaveDef *)(ADDR_START + SAVE_POWERCONFIG * ADDR_SECTOR_SIZE);
	PowDataSaveDef *PowDataWrite = rt_malloc(sizeof(PowDataSaveDef) + 1);
  if(NULL == PowDataWrite)return;
  
	rt_memcpy(PowDataWrite, PowDataFlash, sizeof(PowDataSaveDef));

	if((0x5550 != PowDataWrite->BeginFlag)||(0xaaa0 != PowDataWrite->EndFlag))
	{
		rt_memcpy(PowDataWrite, &PowDataInit, sizeof(PowDataSaveDef));
		
		E_CFG_WriteFlash((uint8_t *)PowDataWrite,ADDR_START + SAVE_POWERCONFIG * ADDR_SECTOR_SIZE,sizeof(PowDataSaveDef));
	}
	rt_free(PowDataWrite);
}

void SetDataSaveInit(void)//1
{//设置参数初始化
	SetDataSaveDef *SetDataFlash = NULL;
	SetDataFlash = (SetDataSaveDef *)(ADDR_START + SAVE_LOGICCONFIG * ADDR_SECTOR_SIZE);
	SetDataSaveDef *SetDataWrite = rt_malloc(sizeof(SetDataSaveDef) + 1);
	if(NULL == SetDataWrite)return;
  
	rt_memcpy(SetDataWrite, SetDataFlash, sizeof(SetDataSaveDef));
	
	if((0x5551 != SetDataWrite->BeginFlag)||(0xaaa1 != SetDataWrite->EndFlag))
	{
		rt_memcpy(SetDataWrite, &SetDataInit, sizeof(SetDataSaveDef));
		
		E_CFG_WriteFlash((uint8_t *)SetDataWrite,ADDR_START + SAVE_LOGICCONFIG * ADDR_SECTOR_SIZE,sizeof(SetDataSaveDef));
	}
	rt_free(SetDataWrite);
}

void TimDataSaveInit(void)//2
{//定时任务参数初始化
	TimDataSaveDef *TimDataFlash = NULL;
	TimDataFlash = (TimDataSaveDef *)(ADDR_START + SAVE_TIMELIST * ADDR_SECTOR_SIZE);
	TimDataSaveDef *TimDataWrite = rt_malloc(sizeof(TimDataSaveDef) + 1);
	if(NULL == TimDataWrite)return;
  
	rt_memcpy(TimDataWrite, TimDataFlash, sizeof(TimDataSaveDef));
	
	if((0x5552 != TimDataWrite->BeginFlag)||(0xaaa2 != TimDataWrite->EndFlag))
	{
		rt_memcpy(TimDataWrite, &TimDataInit, sizeof(TimDataSaveDef));
		
		E_CFG_WriteFlash((uint8_t *)TimDataWrite,ADDR_START + SAVE_TIMELIST * ADDR_SECTOR_SIZE,sizeof(TimDataSaveDef));
	}
	rt_free(TimDataWrite);
}

void SysDataSaveInit(void)//3
{//系统参数初始化
	SysDataSaveDef *SysDataFlash = NULL;
	SysDataFlash = (SysDataSaveDef *)(ADDR_START + SAVE_SYSCONFIG * ADDR_SECTOR_SIZE);
	SysDataSaveDef *SysDataWrite = rt_malloc(sizeof(SysDataSaveDef) + 1);
	if(NULL == SysDataWrite)return;
  
	rt_memcpy(SysDataWrite, SysDataFlash, sizeof(SysDataSaveDef));
	
	if((0x5553 != SysDataWrite->BeginFlag)||(0xaaa3 != SysDataWrite->EndFlag))
	{
		rt_memcpy(SysDataWrite, &SysDataInit, sizeof(SysDataSaveDef));
		E_CFG_WriteFlash((uint8_t *)SysDataWrite, ADDR_START + SAVE_SYSCONFIG * ADDR_SECTOR_SIZE, sizeof(SysDataSaveDef));
	}
	rt_free(SysDataWrite);
}

void NetDataSaveInit(void)//4
{//网络参数初始化
	NetDataSaveDef *NetDataFlash = NULL;
	NetDataFlash = (NetDataSaveDef *)(ADDR_START + SAVE_NETCONFIG * ADDR_SECTOR_SIZE);
	NetDataSaveDef *NetDataWrite = rt_malloc(sizeof(NetDataSaveDef) + 1);
	if(NULL == NetDataWrite)return;
  
	rt_memcpy(NetDataWrite, NetDataFlash, sizeof(NetDataSaveDef));
	
	if((0x5554 != NetDataWrite->BeginFlag)||(0xaaa4 != NetDataWrite->EndFlag))
	{
		rt_memcpy(NetDataWrite, &NetDataInit, sizeof(NetDataSaveDef));
		E_CFG_WriteFlash((uint8_t *)NetDataWrite, ADDR_START + SAVE_NETCONFIG * ADDR_SECTOR_SIZE, sizeof(NetDataSaveDef));
	}
	rt_free(NetDataWrite);
}

void CalDataSaveInit(void)//5
{//校准参数初始化
	CalDataSaveDef *CalDataFlash = NULL;
	CalDataFlash = (CalDataSaveDef *)(ADDR_START + SAVE_AICHACK * ADDR_SECTOR_SIZE);
	CalDataSaveDef *CalDataWrite = rt_malloc(sizeof(CalDataSaveDef) +1);
	if(NULL == CalDataWrite)return;
  
	rt_memcpy(CalDataWrite,CalDataFlash,sizeof(CalDataSaveDef));
	
	if((0x5555 != CalDataWrite->BeginFlag)||(0xaaa5 != CalDataWrite->EndFlag))
	{
		rt_memcpy(CalDataWrite, &CalDataInit, sizeof(CalDataSaveDef));
		E_CFG_WriteFlash((uint8_t *)CalDataWrite, ADDR_START + SAVE_AICHACK * ADDR_SECTOR_SIZE, sizeof(CalDataSaveDef));
	}
	rt_free(CalDataWrite);
}


//void PowDataSave(uint16_t addr, uint8_t *buf, uint16_t size)
//{//掉电参数保存
//	PowDataSaveDef *PowDataFlash = NULL;
//	PowDataFlash = (PowDataSaveDef *)(ADDR_START + SAVE_POWERCONFIG * ADDR_SECTOR_SIZE);
//	PowDataSaveDef *PowDataWrite = rt_malloc(sizeof(PowDataSaveDef) + 1);
//	
//	rt_memcpy(PowDataWrite,PowDataFlash,sizeof(PowDataSaveDef));
//	
//	uint16_t *PowDataBuf = NULL;
//	
//	PowDataBuf = PowDataWrite->Buf;

//	if((0x5550 == PowDataWrite->BeginFlag)&&(0xaaa0 == PowDataWrite->EndFlag))
//	{
//		for(uint8_t i = 0; i < size; i++)
//		{
//			PowDataBuf[addr+i] = ((buf[i*2+1]<<8)|buf[i*2]);
//		}
//		E_CFG_WriteFlash((uint8_t *)PowDataWrite,ADDR_START + SAVE_POWERCONFIG * ADDR_SECTOR_SIZE,sizeof(PowDataSaveDef));
//	}
//	
//	rt_free(PowDataWrite);
//}

void SetDataSave(uint16_t addr, uint8_t *buf, uint16_t size)
{//设置参数保存
	SetDataSaveDef *SetDataFlash = NULL;
	SetDataFlash = (SetDataSaveDef *)(ADDR_START + SAVE_LOGICCONFIG * ADDR_SECTOR_SIZE);
	SetDataSaveDef *SetDataWrite = rt_malloc(sizeof(SetDataSaveDef) + 1);
	if(NULL == SetDataWrite)return;
  
	rt_memcpy(SetDataWrite,SetDataFlash,sizeof(SetDataSaveDef));
	
	uint16_t *SetDataBuf = NULL;
	
	SetDataBuf = (uint16_t *)(&SetDataWrite->MasterSetConfig);
	
	if((0x5551 == SetDataWrite->BeginFlag)&&(0xaaa1 == SetDataWrite->EndFlag))
	{
		for(uint8_t i = 0; i < size; i++)
		{
			SetDataBuf[addr+i] = (buf[i*2]<<8|buf[i*2+1]);
		}
		if(0 != rt_memcmp(SetDataFlash, SetDataWrite, sizeof(SetDataSaveDef)))
		{
			SaveMail.Size = sizeof(SetDataSaveDef);
			SaveMail.Addr = ADDR_START + SAVE_LOGICCONFIG * ADDR_SECTOR_SIZE;
			SaveMail.Buff = (uint8_t *)SetDataWrite;
			while(Mb_FLASH.entry >= Mb_FLASH.size)
			{
				rt_thread_mdelay(10);
			}
			rt_mb_send(&Mb_FLASH, (rt_uint32_t)&SaveMail);
//			E_CFG_WriteFlash((uint8_t *)SetDataWrite, ADDR_START + SAVE_LOGICCONFIG * ADDR_SECTOR_SIZE,	sizeof(SetDataSaveDef));
		}
	}
	rt_free(SetDataWrite);
}
void TimDataSave(uint16_t addr, uint8_t *buf, uint16_t size)
{//定时任务参数保存
	TimDataSaveDef *TimDataFlash = NULL;
	TimDataFlash = (TimDataSaveDef *)(ADDR_START + SAVE_TIMELIST * ADDR_SECTOR_SIZE);
	TimDataSaveDef *TimDataWrite = rt_malloc(sizeof(TimDataSaveDef) + 1);
	if(NULL == TimDataWrite)return;
  
	rt_memcpy(TimDataWrite,TimDataFlash,sizeof(TimDataSaveDef));
	
	uint16_t *TimDataBuf = NULL;
	
	TimDataBuf = (uint16_t *)(&TimDataWrite->sTimeListWork);
	
	if((0x5552 == TimDataWrite->BeginFlag)&&(0xaaa2 == TimDataWrite->EndFlag))
	{
		for(uint8_t i = 0; i < size; i++)
		{
			TimDataBuf[addr+i] = (buf[i*2]<<8|buf[i*2+1]);
		}
		if(0 != rt_memcmp(TimDataFlash, TimDataWrite, sizeof(TimDataSaveDef)))
		{
			SaveMail.Size = sizeof(TimDataSaveDef);
			SaveMail.Addr = ADDR_START + SAVE_TIMELIST * ADDR_SECTOR_SIZE;
			SaveMail.Buff = (uint8_t *)TimDataWrite;
			
			while(Mb_FLASH.entry >= Mb_FLASH.size)
			{
				rt_thread_mdelay(10);
			}
			rt_mb_send(&Mb_FLASH, (rt_uint32_t)&SaveMail);
			//E_CFG_WriteFlash((uint8_t *)TimDataWrite,ADDR_START + SAVE_TIMELIST * ADDR_SECTOR_SIZE,sizeof(TimDataSaveDef));
		}
	}
	rt_free(TimDataWrite);
}

void SysDataSave(uint16_t addr, uint8_t *buf, uint16_t size)
{//系统参数保存
	SysDataSaveDef *SysDataFlash = NULL;
	SysDataFlash = (SysDataSaveDef *)(ADDR_START + SAVE_SYSCONFIG * ADDR_SECTOR_SIZE);
	SysDataSaveDef *SysDataWrite = rt_malloc(sizeof(SysDataSaveDef) + 1);
	if(NULL == SysDataWrite)return;
  
	rt_memcpy(SysDataWrite,SysDataFlash,sizeof(SysDataSaveDef));
	
	uint16_t *SysDataBuf = NULL;
	
	SysDataBuf = (uint16_t *)(&SysDataWrite->SysData);
	
	if((0x5553 == SysDataWrite->BeginFlag)&&(0xaaa3 == SysDataWrite->EndFlag))
	{
		for(uint8_t i = 0; i < size; i++)
		{
			SysDataBuf[addr+i] = (buf[i*2]<<8|buf[i*2+1]);
		}
		if(0 != rt_memcmp(SysDataFlash, SysDataWrite, sizeof(SysDataSaveDef)))
		{
			SaveMail.Size = sizeof(SysDataSaveDef);
			SaveMail.Addr = ADDR_START + SAVE_SYSCONFIG * ADDR_SECTOR_SIZE;
			SaveMail.Buff = (uint8_t *)SysDataWrite;
			
			while(Mb_FLASH.entry >= Mb_FLASH.size)
			{
				rt_thread_mdelay(10);
			}
			rt_mb_send(&Mb_FLASH, (rt_uint32_t)&SaveMail);
			//E_CFG_WriteFlash((uint8_t *)SysDataWrite,ADDR_START + SAVE_SYSCONFIG * ADDR_SECTOR_SIZE,sizeof(SysDataSaveDef));
		}
		
	}
	rt_free(SysDataWrite);
}
void NetDataSave(uint16_t addr, uint8_t *buf, uint16_t size)
{//网络参数保存
	NetDataSaveDef *NetDataFlash = NULL;
	NetDataFlash = (NetDataSaveDef *)(ADDR_START + SAVE_NETCONFIG * ADDR_SECTOR_SIZE);
	NetDataSaveDef *NetDataWrite = rt_malloc(sizeof(NetDataSaveDef) + 1);
	if(NULL == NetDataWrite)return;
  
	rt_memcpy(NetDataWrite,NetDataFlash,sizeof(NetDataSaveDef));
	
	uint16_t *NetDataBuf = NULL;
	
	NetDataBuf = NetDataWrite->Buf;
	
	if((0x5554 == NetDataWrite->BeginFlag)&&(0xaaa4 == NetDataWrite->EndFlag))
	{
		for(uint8_t i = 0; i < size; i++)
		{
			NetDataBuf[addr+i] = (buf[i*2]<<8|buf[i*2+1]);
		}
		if(0 != rt_memcmp(NetDataFlash, NetDataWrite, sizeof(NetDataSaveDef)))
		{
			SaveMail.Size = sizeof(NetDataSaveDef);
			SaveMail.Addr = ADDR_START + SAVE_NETCONFIG * ADDR_SECTOR_SIZE;
			SaveMail.Buff = (uint8_t *)NetDataWrite;
			
			while(Mb_FLASH.entry >= Mb_FLASH.size)
			{
				rt_thread_mdelay(10);
			}
			rt_mb_send(&Mb_FLASH, (rt_uint32_t)&SaveMail);
			//E_CFG_WriteFlash((uint8_t *)NetDataWrite,ADDR_START + SAVE_NETCONFIG * ADDR_SECTOR_SIZE, sizeof(NetDataSaveDef));
		}
	}
	rt_free(NetDataWrite);
}
void CalDataSave(uint16_t addr, uint8_t *buf, uint16_t size)
{//校准参数保存
	CalDataSaveDef *CalDataFlash = NULL;
	CalDataFlash = (CalDataSaveDef *)(ADDR_START + SAVE_AICHACK * ADDR_SECTOR_SIZE);
	CalDataSaveDef *CalDataWrite = rt_malloc(sizeof(CalDataSaveDef));
	if(NULL == CalDataWrite)return;
  
	rt_memcpy(CalDataWrite,CalDataFlash,sizeof(CalDataSaveDef));
	
	uint16_t *CalDataBuf = (uint16_t *)(CalDataWrite->Buf);
	
	
	
	if((0x5555 == CalDataWrite->BeginFlag)&&(0xaaa5 == CalDataWrite->EndFlag))
	{
		for(uint8_t i = 0; i < size; i++)
		{
			CalDataBuf[addr+i] = (buf[i*2]<<8|buf[i*2+1]);
		}
		if(0 != rt_memcmp(CalDataFlash, CalDataWrite, sizeof(CalDataSaveDef)))
		{
			SaveMail.Size = sizeof(CalDataSaveDef);
			SaveMail.Addr = ADDR_START + SAVE_AICHACK * ADDR_SECTOR_SIZE;
			SaveMail.Buff = (uint8_t *)CalDataWrite;
			
			while(Mb_FLASH.entry >= Mb_FLASH.size)
			{
				rt_thread_mdelay(10);
			}
			rt_mb_send(&Mb_FLASH, (rt_uint32_t)&SaveMail);
		}
	}
	rt_free(CalDataWrite);
}

void MqcpTimSave(sTimeListDef *sTimeListWork, uint16_t Id, uint16_t state)
{//MQCP    定时任务参数保存
	TimDataSaveDef *TimDataFlash = NULL;
	TimDataFlash = (TimDataSaveDef *)(ADDR_START + SAVE_TIMELIST * ADDR_SECTOR_SIZE);
	TimDataSaveDef *TimDataWrite = rt_malloc(sizeof(TimDataSaveDef) + 1);
	if(NULL == TimDataWrite)return;
  
	rt_memcpy(TimDataWrite,TimDataFlash,sizeof(TimDataSaveDef));
	
	if((0x5552 == TimDataWrite->BeginFlag)&&(0xaaa2 == TimDataWrite->EndFlag))
	{
		rt_memcpy((uint8_t *)(&(TimDataWrite->sTimeListWork.TimeList[Id].Id)),sTimeListWork,sizeof(sTimeListDef));
		if((1 == state)&&(TimDataWrite->sTimeListWork.TimeListSize < TimDataWrite->sTimeListWork.TimeListSizeMax))
		{
			TimDataWrite->sTimeListWork.TimeListSize++;
		}
		else if((2 == state)&&(TimDataWrite->sTimeListWork.TimeListSize > 0))
		{
			TimDataWrite->sTimeListWork.TimeListSize--;
		}
		
//		if(0 != rt_memcmp(TimDataFlash, TimDataWrite, sizeof(SysDataSaveDef)))
//		{
			SaveMail.Size = sizeof(TimDataSaveDef);
			SaveMail.Addr = ADDR_START + SAVE_TIMELIST * ADDR_SECTOR_SIZE;
			SaveMail.Buff = (uint8_t *)TimDataWrite;
			
			while(Mb_FLASH.entry >= Mb_FLASH.size)
			{
				rt_thread_mdelay(10);
			}
			rt_mb_send(&Mb_FLASH, (rt_uint32_t)&SaveMail);
			//E_CFG_WriteFlash((uint8_t *)TimDataWrite,ADDR_START + SAVE_TIMELIST * ADDR_SECTOR_SIZE,sizeof(TimDataSaveDef));
//		}
	}
	rt_free(TimDataWrite);
}

void MqcpSetSave(uint16_t addr, uint8_t *buf, uint16_t size)
{//MQCP    设置参数保存
	SetDataSaveDef *SetDataFlash = NULL;
	SetDataFlash = (SetDataSaveDef *)(ADDR_START + SAVE_LOGICCONFIG * ADDR_SECTOR_SIZE);
	SetDataSaveDef *SetDataWrite = rt_malloc(sizeof(SetDataSaveDef) + 1);
	if(NULL == SetDataWrite)return;
	
	rt_memcpy(SetDataWrite,SetDataFlash,sizeof(SetDataSaveDef));
	
	uint16_t *SetDataBuf = NULL;
	
	SetDataBuf = (uint16_t *)(&SetDataWrite->MasterSetConfig);
	
	if((0x5551 == SetDataWrite->BeginFlag)&&(0xaaa1 == SetDataWrite->EndFlag))
	{
		for(uint8_t i = 0; i < size; i++)
		{
			SetDataBuf[addr+i] = (buf[i*2+1]<<8|buf[i*2]);
		}
		if(0 != rt_memcmp(SetDataFlash, SetDataWrite, sizeof(SetDataSaveDef)))
		{
			SaveMail.Size = sizeof(SetDataSaveDef);
			SaveMail.Addr = ADDR_START + SAVE_LOGICCONFIG * ADDR_SECTOR_SIZE;
			SaveMail.Buff = (uint8_t *)SetDataWrite;
			while(Mb_FLASH.entry >= Mb_FLASH.size)
			{
				rt_thread_mdelay(10);
			}
			rt_mb_send(&Mb_FLASH, (rt_uint32_t)&SaveMail);
//			E_CFG_WriteFlash((uint8_t *)SetDataWrite, ADDR_START + SAVE_LOGICCONFIG * ADDR_SECTOR_SIZE,	sizeof(SetDataSaveDef));
		}
	}
	rt_free(SetDataWrite);
}
/* Private function prototypes --------私有子函数---------------------------------------*/

/* USER CODE BEGIN PFP */




/* USER CODE END PFP */


/* Private user code ------------------私有用户程序---------------------------------------*/
/* USER CODE BEGIN  */

void FlashSaveTask(void *p)
{
	PowDataSaveInit();
	SetDataSaveInit();
	TimDataSaveInit();
	SysDataSaveInit();
	NetDataSaveInit();
	CalDataSaveInit();
	
	SaveMailDef *FlashValue;
	
	while(1)
	{
		if(RT_EOK == rt_mb_recv(&Mb_FLASH, (rt_uint32_t *)&FlashValue, RT_WAITING_FOREVER))//FLASH 邮箱接收
		{//FLASH
			E_CFG_WriteFlash(FlashValue->Buff, FlashValue->Addr,	FlashValue->Size);
		}
	}
}

void ConfigThreadCreate(void)
{
	{//邮件创建
		rt_err_t result;
		result =  rt_mb_init(&Mb_FLASH,									//邮箱对象的句柄
												 "Mb_FLASH",								//邮箱名称
												 &Mb_FLASH_Pool[0], 				//缓冲区指针
												 sizeof(Mb_FLASH_Pool)/4,		//邮箱容量
												 RT_IPC_FLAG_FIFO );				//邮箱标志先到先得
		if (result != RT_EOK)
		{
			return;
		}
	}
	{//线程创建
		rt_thread_t tid;
		tid = rt_thread_create("ConfigTask",
                           FlashSaveTask, RT_NULL,
                           CONFIG_STACK_SIZE,
                           CONFIG_PRIORITY,
                           CONFIG_TIMESLICE);
		if (tid != RT_NULL)
		{
					rt_thread_startup(tid);
		}
	}
}

/* USER CODE END  */





/************************ (C) COPYRIGHT CunJu *****END OF FILE****/
